<?php

namespace Database\Seeders;

use App\Models\Sentence\Sentence;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SentenceDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $sentences = DB::connection('rjmysql')->table('often_expressions')->get()->toArray();
        $sentences = json_decode(json_encode($sentences), true);

        foreach ($sentences as $sentence) {
            if (DB::table('categorys')->where('id', $sentence['category_id'])->exists() &&
                DB::table('click_learns')->where('id', $sentence['click_learn_id'])->exists()) {

                Sentence::query()->updateOrCreate(
                    ['id' => $sentence['id']],
                    $sentence
                );
            }
        }

        $this->command->info('常用表达法数据填充完成！');
    }
}
